package com.github.natholdallas.etc

import cn.dev33.satoken.interceptor.SaInterceptor
import cn.dev33.satoken.router.SaRouter
import cn.dev33.satoken.stp.StpUtil
import com.github.natholdallas.common.AViews
import com.github.natholdallas.common.ApiViews
import com.github.natholdallas.common.CViews
import com.github.natholdallas.common.UViews
import org.springframework.context.annotation.Configuration
import org.springframework.web.servlet.config.annotation.InterceptorRegistry
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer

@Configuration
open class WebMvcConfig : WebMvcConfigurer {

    override fun addInterceptors(registry: InterceptorRegistry) {
        registry
            .addInterceptor(SaInterceptor {
                SaRouter.match("/admin/api/v1/**", StpUtil::checkLogin)
                SaRouter.match("/user/api/v1/**", StpUtil::checkLogin)
                SaRouter.match("/copilot/api/v1/**", StpUtil::checkLogin)
            })
            .addPathPatterns("/**")
    }

    override fun configurePathMatch(configurer: PathMatchConfigurer) {
        configurer
            .addPathPrefix("/api/v1") { it.isAnnotationPresent(ApiViews::class.java) }
            .addPathPrefix("/admin/api/v1") { it.isAnnotationPresent(AViews::class.java) }
            .addPathPrefix("/user/api/v1") { it.isAnnotationPresent(UViews::class.java) }
            .addPathPrefix("/copilot/api/v1") { it.isAnnotationPresent(CViews::class.java) }
    }

}
